<template>
  <div>
    <el-dialog
      v-loading.lock="loading"
      class="base-dialog system-user-dialog"
      :custom-class="'panna-dialog_big'"
      :append-to-body="true"
      :title="dataList.AddTaskInfo"
      align="center"
      :close-on-click-modal="false"
      :visible.sync="innerDialogFormVisible"
    >
      <el-divider class="el-divider-specail" content-position="left"
        ><span>{{ dataList.VerifyEquip }}</span>
        <el-button
          type="primary"
          style="margin-left: 20px"
          @click="choiceDevice"
          >{{ dataList.SelectDevice }}</el-button
        >
      </el-divider>
      <el-table
        :data="list"
        element-loading-text="Loading"
        fit
        highlight-current-row
        height="130px"
      >
        >
        <el-table-column :label="dataList.DeviceNumber">
          <template slot-scope="scope">
            {{ scope.row.num }}
          </template>
        </el-table-column>
        <el-table-column
          :label="
            $i18n.locale === 'en' ? dataList.DeviceEName : dataList.DeviceName
          "
        >
          <template slot-scope="scope">
            {{ scope.row.name }}
          </template>
        </el-table-column>
        <el-table-column :label="dataList.DeviceSpecifications">
          <template slot-scope="scope">
            {{ scope.row.specifications }}
          </template>
        </el-table-column>
        <el-table-column :label="dataList.DeviceStatus">
          <template slot-scope="scope">
            {{ scope.row.statusName }}
          </template>
        </el-table-column>
        <el-table-column
          :label="
            $i18n.locale === 'en'
              ? dataList.eDeviceStorageAddress
              : dataList.DeviceStorageAddress
          "
        >
          <template slot-scope="scope">
            {{ scope.row.storageAddress }}
          </template>
        </el-table-column>
        <el-table-column :label="dataList.DevicePersonResponsibleId">
          <template slot-scope="scope">
            {{ scope.row.personResponsibleName }}
          </template>
        </el-table-column>
        <el-table-column :label="dataList.Operation">
          <template slot-scope="scope">
            <div class="base-table-operation">
              <el-link
                size="mini"
                type="danger"
                @click="handleDelete(scope.$index)"
                >{{ btn.Delete }}</el-link
              >
            </div>
          </template>
        </el-table-column>
      </el-table>

      <el-divider class="el-divider-specail" content-position="left"
        ><span>{{ dataList.CalibrationPlanProgramme }}</span>
      </el-divider>
      <el-form
        ref="formData"
        class="base-pipeline-layout system-user-form"
        :label-width="$i18n.locale === 'en' ? '180px' : '100px'"
        :model="formData"
        :rules="formDataRules"
        status-icon
      >
        <el-form-item
          style="width: 100%"
          :label="dataList.CalibrationPersonName"
          prop="calibrationPersonId"
          :error="errorMsg.calibrationPersonId"
        >
          <div style="width: 30%; float: left">
            <UserDropDownList
              :value="formData.calibrationPersonId"
              :return-objct.sync="returnObjct"
              @input="borrowPersonChange"
            />
          </div>
        </el-form-item>
        <el-form-item
          style="width: 100%"
          :label="dataList.CalibrationTime"
          prop="planDate"
          :error="errorMsg.planDate"
        >
          <el-date-picker
            v-model="formData.planDate"
            style="width: 30%; float: left"
            type="date"
            :placeholder="dataList.SelectDateTime"
            value-format="yyyy-MM-dd"
          ></el-date-picker>
        </el-form-item>
        <el-form-item
          style="width: 60%"
          :label="dataList.CalibrationProcedure2"
          prop="verificationObject"
          :error="errorMsg.verificationObject"
        >
          <el-input
            v-model="formData.verificationObject"
            type="textarea"
          ></el-input>
        </el-form-item>
        <el-form-item
          style="width: 60%"
          :label="dataList.CalibrationItem"
          prop="verificationWay"
          :error="errorMsg.verificationWay"
        >
          <el-input
            v-model="formData.verificationWay"
            type="textarea"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" align="center">
        <el-button type="primary" @click="onSubmit(10)">{{
          btn.Save
        }}</el-button>
        <el-button @click="cancel">{{ btn.Cancel }}</el-button>
      </div>
    </el-dialog>
    <DeviceDialogList
      v-if="choiceDeviceVisible"
      :dialog-form-visible.sync="choiceDeviceVisible"
      :choice-device-num.sync="choiceDeviceNum"
      :not-display-text.sync="notDisplayText"
      :query-form-parent.sync="queryFormParent"
      @checkedRowsFun="closeChoiceDevice"
    />
  </div>
</template>

<script>
import { create, update } from "../../../api/DeviceCalibrationTask.js";
import DeviceDialogList from "../../../components/DeviceDialogList.vue";
import UserDropDownList from "../../../components/UserDropDownList.vue";
import { scrap } from "../../../utils/const";

export default {
  name: "DeviceCalibrationTaskAddDialog",
  components: { DeviceDialogList, UserDropDownList },
  props: {
    dialogFormVisible: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      list: [],
      queryFormParent: {
        useSymbolStatusId: scrap,
        symbol: "!=",
      },
      notDisplayText: [ this.$t("ModuleDevice.Device.Scrap")],
      choiceDeviceNum: 1,
      returnObjct: true,
      loading: false,
      choiceDeviceVisible: false,
      innerDialogFormVisible: false,
      formLabelWidth: "100px",
      initData: {
        id: 0,
        planDate: null,
        deviceId: 0,
        deviceNum: "",
        calibrationPersonId: null,
        calibrationPersonName: "",
        verificationObject: "",
        verificationWay: "",
      },
      formData: {
        id: 0,
        planDate: new Date(),
        calibrationPersonId: null,
        calibrationPersonName: "",
        verificationObject: "",
        verificationWay: "",
      },
      formDataRules: {
        calibrationPersonId: [
          {
            required: true,
            trigger: "blur",
            validator: this.Helper.validateFunction,
          },
        ],
        planDate: [
          {
            required: true,
            trigger: "blur",
            validator: this.Helper.validateFunction,
          },
        ],
        verificationObject: [
          {
            required: true,
            trigger: "blur",
            validator: this.Helper.validateFunction,
          },
        ],
        verificationWay: [
          {
            required: true,
            trigger: "blur",
            validator: this.Helper.validateFunctionPositiveNumber,
          },
        ],
      },
      initErrorMsg: {
        calibrationPersonId: "",
        calibrationPersonName: "",
        verificationObject: "",
        verificationWay: "",
      },
      errorMsg: {
        calibrationPersonId: "",
        calibrationPersonName: "",
        verificationObject: "",
        verificationWay: "",
      },
    };
  },
  computed: {
    btn() {
      return this.$t("ModuleDevice.buttons");
    },
    dataList() {
      return this.$t("ModuleDevice.Device");
    },
  },
  watch: {
    dialogFormVisible(val) {
      this.innerDialogFormVisible = val;
    },
    innerDialogFormVisible(val) {
      this.$emit("update:dialogFormVisible", val);
    },
  },
  created() {
    this.innerDialogFormVisible = this.dialogFormVisible;
  },
  methods: {
    handleDelete(index) {
      this.list.splice(index, 1);
    },
    // 关闭选择设备弹框
    closeChoiceDevice(data) {
      this.formData.deviceId = data[0].id;
      this.formData.deviceNum = data[0].num;

      this.list = data;
    },
    borrowPersonChange(val) {
      this.formData.calibrationPersonId = val.value;
      this.formData.calibrationPersonName = val.label;
    },
    // 选择设备
    choiceDevice() {
      this.choiceDeviceVisible = true;
    },
    cancel() {
      this.innerDialogFormVisible = false;
    },
    closeDialog() {
      this.$parent.getList();
      this.cancel();
    },
    onSubmit(status) {
      this.formData.status = status;
      this.formData.type = 20;
      this.$refs.formData.validate((valid) => {
        if (valid) {
          this.loading = true;
          this.errorMsg = this.Helper.deepCopy(this.initErrorMsg);
          if (this.formData.id <= 0) {
            create(this.formData)
              .then((res) => {
                if (res.code === 200) {
                  this.closeDialog();
                } else {
                  this.showValidateMsgCheck(res);
                }
                this.loading = false;
              })
              .catch(() => {
                this.loading = false;
              });
          } else {
            update(this.formData)
              .then((res) => {
                if (res.code === 200) {
                  this.closeDialog();
                } else {
                  this.showValidateMsgCheck(res);
                }
                this.loading = false;
              })
              .catch(() => {
                this.loading = false;
              });
          }
        } else {
          return false;
        }
      });
    },
    showValidateMsgCheck(res) {
      if (res.msg) {
        this.Helper.showValidateMsg(
          "calibrationPersonId",
          this.errorMsg,
          res.msg
        );
        this.Helper.showValidateMsg(
          "calibrationPersonName",
          this.errorMsg,
          res.msg
        );
        this.Helper.showValidateMsg(
          "verificationObject",
          this.errorMsg,
          res.msg
        );
        this.Helper.showValidateMsg("verificationWay", this.errorMsg, res.msg);
      }
    },
  },
};
</script>
